package com.turing.data.service.review.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.turing.data.dao.review.ReviewContentMapper;
import com.turing.data.inner.params.out.ElementDetailVO;
import com.turing.data.model.domain.review.ReviewContent;
import com.turing.data.service.element.ElementService;
import com.turing.data.service.review.ReviewContentStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReviewContentStorageServiceImpl implements ReviewContentStorageService {

    @Autowired
    private ReviewContentMapper reviewContentMapper;

    @Autowired
    private ElementService elementService;

    @Value("${review.output.path}")
    public String STORAGE_PATH;

    public static void main(String[] args) {
        String key = "00104";
        String dir = key.substring(0, key.length() - 1);
        String child = key.substring(key.length() - 1);

        System.out.println(dir);
        System.out.println(child);
    }

    public void doOutputReviewFile(Integer version) {
        List<ReviewContent> reviewContents = checkOutReviewContent(version);
        Map<String, List<ReviewContent>> mapped = groupedReviewContent(reviewContents);
        String versionPath = STORAGE_PATH + "version_" + version;

        File versionFile = new File(versionPath);
        if (!versionFile.exists()) {
            versionFile.mkdir();
        }
        for (String key : mapped.keySet()) {

            // 策略模式来做, 根据key的不同类型来存
            String dir = key.substring(0, key.length() - 1);
            String child = key.substring(key.length() - 1);

            String parentPath = versionPath + "/" + dir;
            File dirPath = new File(parentPath);
            if (!dirPath.exists()) {
                boolean mkdirTag = dirPath.mkdir();
            }

            String childFilePath = parentPath + "/" + key + ".json";
            File childFile = new File(childFilePath);
            if (!childFile.exists()) {
                try {
                    childFile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            JSONObject eleMap = new JSONObject();

            List<ReviewContent> contents = mapped.get(key);
            for (int i = 0; i < contents.size(); i++) {
                ReviewContent reviewContent = contents.get(i);
                Integer eleId = reviewContent.getEleId();
                ElementDetailVO elementDetail = elementService.getElementDetailById(eleId);
                String eleName = elementDetail.getEleName();
            }


            FileWriter fileWriter = null;
            try {
                fileWriter = new FileWriter(childFile.getAbsoluteFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
            assert fileWriter != null;
            BufferedWriter bw = new BufferedWriter(fileWriter);
            try {
                bw.write(eleMap.toJSONString());
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                bw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("finish");
        }
    }

    public List<ReviewContent> checkOutReviewContent(Integer version) {
        return new LambdaQueryChainWrapper<>(reviewContentMapper)
                .list();
    }

    public Map<String, List<ReviewContent>> groupedReviewContent(List<ReviewContent> reviewContents) {
        return reviewContents
                .parallelStream()
                .collect(Collectors.groupingBy(ReviewContent::getTaskSn));
    }

}
